import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-car',
  templateUrl: './car.component.html',
  styleUrls: ['./car.component.less']
})
export class CarComponent implements OnInit {
  value = '';

  constructor(private http: HttpClient) { }

  ngOnInit(): void {
  }

  up(): void {
    this.send('up');
  }

  down(): void {
    this.send('down');
  }

  stop(): void {
    this.send('stop');
  }

  left(): void {
    this.send('left');
  }

  right(): void {
    this.send('right');
  }

  validate(): void {

  }

  send(direction: string): void {
    try {
      const url = 'http://' + this.value;
      const body = {
        message: direction
      };
      this.http.post(url, JSON.stringify(body)).toPromise().then(resp => {
        console.log(resp);
      }).catch(err => {
        console.log(err);
      });
    } catch (error) {
      console.error(error);
    }
  }

}
